package tianrun.ziguan.api.calculate.config.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil extends StringUtils {
    protected static final Map<Integer, String> CHINESE_MAP = new HashMap<>();
    protected static final Map<String, Integer> NUM_MAP = new HashMap<>();
    private static final Pattern linePattern = Pattern.compile("_(\\w)");

    private StringUtil() {
    }

    public static boolean isNotEqualsIgnoreCase(String source, String target) {
        return !equalsIgnoreCase(source, target);
    }

    private static final Pattern humpPattern = Pattern.compile("[A-Z]");

    public static String upperFirstLatter(String letter) {
        return letter.substring(0, 1).toUpperCase() + letter.substring(1);
    }

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转下划线,效率比上面高
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString().toUpperCase();
    }

    public static String getFileId(String fileUrl) {
        if (fileUrl == null) {
            return null;
        }
        int i = fileUrl.lastIndexOf('/');

        if (i == -1) {
            return null;
        }
        if (i == fileUrl.length() - 1) {//判断是否是最后的 '/'
            return null;
        }
        return fileUrl.substring(i + 1);
    }

    public static boolean isNotNullAndBlank(Object o) {
        return Objects.nonNull(o) && StringUtil.isNotBlank(o.toString());
    }

    public static boolean isNullOrBlank(Object o) {
        return !isNotNullAndBlank(o);
    }

    public static String removeBlank(String str) {
        return str.replaceAll("\\s*", "");
    }

    /**
     * 得到字符串，或空串
     *
     * @param value 字符串值对象
     * @return 结果
     */
    public static String valueOrEmpty(String value) {
        if (StringUtil.isEmpty(value)) {
            return "";
        }
        return value;
    }
}
